package com.csht.service.Impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csht.model.City;
import com.csht.model.CshtLunbo;
import com.csht.model.Province;
import com.csht.model.School;
import com.csht.service.CshtGoodsService;
import com.csht.service.CshtLunboService;
import com.csht.service.CshtNoticeService;
import com.csht.service.CshtOneMenuService;
import com.csht.service.CshtShelvesService;
import com.csht.service.CshtTenGoodsEverydayService;
import com.csht.service.CshtTradingService;
import com.csht.service.CshtTradingSuccessService;
import com.csht.service.CshtTwoMenuService;
import com.csht.service.IndexService;
import com.csht.util.HttpRequest;
import com.csht.vo.index.menu.CshtOneMenuVo;
import com.csht.vo.index.shangpinzhangqu.CshtGoodsVo_2;
import com.csht.vo.index.shangpinzhangqu.CshtTwoMenuVo_2;
import com.csht.vo.index.shangpinzhangqu.ShangPinZhangQu;
import com.csht.vo.index.tenGoods.CshtGoodsVo;
import com.csht.vo.index.tenGoods.CshtTenGoodsEverydayVo;

import net.sf.json.JSONArray;

@Service
public class IndexServiceImpl implements IndexService {
	
	@Autowired
	private CshtOneMenuService cshtOneMenuService;
	
	@Autowired
	private CshtLunboService cshtLunboService;
	
	@Autowired
	private CshtTenGoodsEverydayService cshtTenGoodsEverydayService;
	
	@Autowired
	private CshtGoodsService cshtGoodsService;
	
	@Autowired
	private CshtTwoMenuService cshtTwoMenuService;
	
	@Autowired
	private CshtNoticeService cshtNoticeService;
	
	@Autowired
	private CshtTradingService cshtTradingService;
	
	@Autowired
	private CshtTradingSuccessService cshtTradingSuccessService;
	
	@Autowired
	private CshtShelvesService cshtShelvesService;

	/**
	 * 获取全部省份
	 */
	@Override
	public List<Province> getAllProvince(String provinceUrl) throws Exception {
		String result = HttpRequest.sendPost(provinceUrl, "");
		//json字符串转化list
		JSONArray json = JSONArray.fromObject(result);//result是json字符串
		@SuppressWarnings("unchecked")
		List<Province> provinceList= (List<Province>)JSONArray.toList(json, Province.class);
		return provinceList;
	}

	/**
	 * 根据省id获取该省全部城市
	 */
	@Override
	public List<City> getAllCityByProvinceId(String cityUrl, Integer provinceId) throws Exception {
		String result = HttpRequest.sendPost(cityUrl, "id=" + provinceId);
		//json字符串转化list
		JSONArray json = JSONArray.fromObject(result);//result是json字符串
		@SuppressWarnings("unchecked")
		List<City> cityList= (List<City>)JSONArray.toList(json, City.class);
		return cityList;
	}

	/**
	 * 根据城市名获取学校
	 */
	@Override
	public List<School> getAllSchoolByCityName(String schoolUrl, String cityName) throws Exception {
		String result = HttpRequest.sendPost(schoolUrl, "name=" + cityName);
		//json字符串转化list
		JSONArray json = JSONArray.fromObject(result);//result是json字符串
		@SuppressWarnings("unchecked")
		List<School> schoolList= (List<School>)JSONArray.toList(json, School.class);
		return schoolList;
	}

	/**
	 * 获取一、二、三级菜单
	 */
	@Override
	public List<CshtOneMenuVo> getAllMenu() throws Exception {
		List<CshtOneMenuVo> cshtOneMenuVoList = cshtOneMenuService.getOneOrTwoOrThreeMenu();
		return cshtOneMenuVoList;
	}

	/**
	 * 获取轮播图片
	 */
	@Override
	public List<CshtLunbo> getLunbo() throws Exception {
		List<CshtLunbo> cshtLunboList = cshtLunboService.getLunbo();
		return cshtLunboList;
	}

	/**
	 * 每日十件商品扩展
	 */
	@Override
	public List<CshtTenGoodsEverydayVo> getTenGoods() throws Exception {
		List<CshtTenGoodsEverydayVo> cshtTenGoodsEverydayVoList = cshtTenGoodsEverydayService.getTenGoodsEveryday();
		return cshtTenGoodsEverydayVoList;
	}

	/**
	 * 首页商品展区数据
	 */
	@Override
	public List<ShangPinZhangQu> getShangPinZhangQu() throws Exception {
		List<ShangPinZhangQu> shangPinZhangQu = cshtOneMenuService.getOneOrTwoMenuAndBanner();
		return shangPinZhangQu;
	}

	/**
	 * 有用户登录时，随机查询十条商品记录封装到每日十件区
	 */
	@Override
	public List<CshtGoodsVo> getTenGoodsToLoginUser(String goodsTradeSchool) throws Exception {
		List<CshtGoodsVo> cshtGoodsVoList = cshtGoodsService.getTenGoodsToLoginUser(goodsTradeSchool);
		return cshtGoodsVoList;
	}

	/**
	 * 有用户登录时，商品展区获取一级菜单
	 */
	@Override
	public List<ShangPinZhangQu> getOneMenuAndBanner() throws Exception {
		List<ShangPinZhangQu> shangPinZhangQuList = cshtOneMenuService.getOneMenuAndBanner();
		return shangPinZhangQuList;
	}

	/**
	 * 有用户登录时,商品展区获取二级菜单
	 */
	@Override
	public List<CshtTwoMenuVo_2> getTTMG(Integer userId) throws Exception {
		List<CshtTwoMenuVo_2> cshtTwoMenuVo_2List = cshtTwoMenuService.getTTMG(userId);
		return cshtTwoMenuVo_2List;
	}

	/**
	 * 根据用户id查询未读消息数量
	 */
	@Override
	public Integer getNoticeNumberByUserId(Integer userId) throws Exception {
		Integer count = cshtNoticeService.getNoticeNumberByUserId(userId);
		return count;
	}

	/**
	 * 根据用户id查找该用户正在出售商品的数量
	 */
	@Override
	public Integer getGoodsNumberByLoginUser(Integer userId) throws Exception {
		Integer count = cshtGoodsService.getGoodsNumberByLoginUser(userId);
		return count;
	}
	
	/**
	 * 根据用户id查找正处于交易中的商品数量
	 */
	@Override
	public Integer getTradingGoodsNumberByUserId(Integer userId) throws Exception {
		Integer count = cshtTradingService.getTradingGoodsNumberByUserId(userId);
		return count;
	}
	
	/**
	 * 根据用户id查找交易成功的数量
	 */
	@Override
	public Integer getTradingSuccessNumberByUserId(Integer userId) throws Exception {
		Integer count = cshtTradingSuccessService.getTradingSuccessNumberByUserId(userId);
		return count;
	}
	
	/**
	 * 根据用户id查找已下架的商品数量
	 */
	@Override
	public Integer getShelvesNumberByUserId(Integer userId) throws Exception {
		Integer count = cshtShelvesService.getShelvesNumberByUserId(userId);
		return count;
	}
	
	/**
	 * 根据定位地址变化查找商品（每日十件区）
	 */
	@Override
	public List<CshtGoodsVo> getGoodsByAddress(String address) throws Exception {
		List<CshtGoodsVo> cshtGoodsVoList = cshtGoodsService.getGoodsByAddress(address);
		return cshtGoodsVoList;
	}
	
	/**
	 * 定位改变时，根据二级菜单id和用户地址查找商品
	 */
	@Override
	public List<CshtGoodsVo_2> getGoodsByTwoMenuIdAndposition(Map<String, String> tIdAndPName) throws Exception {
		List<CshtGoodsVo_2> cshtGoodsVo_2List = cshtGoodsService.getGoodsByTwoMenuIdAndposition(tIdAndPName);
		return cshtGoodsVo_2List;
	}
	
	/**
	 * (定位改变)商品展区获取一级菜单（封装二级菜单和banner图）
	 */
	@Override
	public List<ShangPinZhangQu> getOneMenu() throws Exception {
		List<ShangPinZhangQu> shangPinZhangQuList =  cshtOneMenuService.getOneMenu();
		return shangPinZhangQuList;
	}

}
